package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.CourseType;
import com.xmy.cultivate.entity.PatriarchSatisfy;
import com.xmy.cultivate.entity.Role;
import com.xmy.cultivate.entity.SatisfiedStatus;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.mapper.PatriarchSatisfyMapper;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.mapper.RepairCourseMapper;
import com.xmy.cultivate.service.IPatriarchSatisfyService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.util.StringUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.ws.rs.POST;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 家长满意度调查 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-06-12
 */
@RestController
@RequestMapping("/api/patriarchSatisfy")
@Scope("prototype")
public class PatriarchSatisfyController extends BaseController{

    @Autowired
    IPatriarchSatisfyService iPatriarchSatisfyService;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    RepairCourseMapper repairCourseMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    PatriarchSatisfyMapper patriarchSatisfyMapper;


    @GetMapping("getSatisfyList")
    @ApiImplicitParam("分页获取满意度列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum" , value = "分页数", dataType = "String" , required =false),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸", dataType = "String" , required =false),
            @ApiImplicitParam(name = "schoolId" , value = "校区id", dataType = "String" , required =false),
            @ApiImplicitParam(name = "subjectsId" , value = "科目", dataType = "String" , required =false),
            @ApiImplicitParam(name = "yearPart" , value = "年份", dataType = "String" , required =false),
            @ApiImplicitParam(name = "quarterNum" , value = "学期", dataType = "String" , required =false),
            @ApiImplicitParam(name = "gradeId" , value = "班级", dataType = "String" , required =false),
            @ApiImplicitParam(name = "evaluateType" , value = "满意情况", dataType = "String" , required =false),
            @ApiImplicitParam(name = "studentName" , value = "学生名", dataType = "String" , required =true),
            @ApiImplicitParam(name = "nextQuartarNum" , value = "报读学期", dataType = "String" , required =true),
    })
    public Result getSatisfyList(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<PatriarchSatisfy> iPage = new Page<>(pageNum,pageSize);

        String schoolId = (String) reMap.get("schoolId");
        String subjectsId = (String) reMap.get("subjectsId");
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String gradeId = (String) reMap.get("gradeId");
        String evaluateType = (String) reMap.get("evaluateType");
        String studentName = (String) reMap.get("studentName");
        String nextQuartarNum = (String) reMap.get("nextQuartarNum") ;
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num",quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq(StringUtils.isNotBlank(evaluateType),"evaluate_type",evaluateType);
        queryWrapper.like(StringUtils.isNotBlank(studentName),"student_name",studentName);
        queryWrapper.eq(StringUtils.isNotBlank(nextQuartarNum),"next_quartar_num",nextQuartarNum);
        iPatriarchSatisfyService.page(iPage,queryWrapper);
        List<PatriarchSatisfy> patriarchSatisfyList = iPage.getRecords();
        for(PatriarchSatisfy patriarchSatisfy : patriarchSatisfyList){
            patriarchSatisfy.setTeacherName(repairCourseMapper.getTeacherName(String.valueOf(patriarchSatisfy.getTeacherId())));
            patriarchSatisfy.setSchoolName(repairCourseMapper.getSchoolName(String.valueOf(patriarchSatisfy.getSchoolId())));
            patriarchSatisfy.setGradeName(gradeMapper.getGradeName(patriarchSatisfy.getGradeId()));
        }
        return new Result(Code.OK,iPage,"满意度情况检索完成");
    }
    @PostMapping("updateSatisfy")
    @ApiImplicitParam("更新满意度信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "id", dataType = "String" , required =false),
            @ApiImplicitParam(name = "schoolId" , value = "校区id", dataType = "String" , required =false),
            @ApiImplicitParam(name = "subjectsId" , value = "科目", dataType = "String" , required =false),
            @ApiImplicitParam(name = "yearClassId" , value = "年级", dataType = "String" , required =false),
            @ApiImplicitParam(name = "yearPart" , value = "年份", dataType = "String" , required =false),
            @ApiImplicitParam(name = "quarterNum" , value = "学期", dataType = "String" , required =false),
            @ApiImplicitParam(name = "gradeId" , value = "班级", dataType = "String" , required =false),
            @ApiImplicitParam(name = "evaluateType" , value = "满意情况", dataType = "String" , required =false),
            @ApiImplicitParam(name = "teacherId" , value = "学生名", dataType = "String" , required =true),
    })
    public Result updateSatisfy(@RequestBody Map reMap){
        String id = (String) reMap.get("id");
        String evaluateType = (String) reMap.get("evaluateType");

        PatriarchSatisfy patriarchSatisfy = iPatriarchSatisfyService.getById(id);
        if(!Objects.isNull(evaluateType)){
            patriarchSatisfy.setEvaluateType(Integer.valueOf(evaluateType));
        }
        patriarchSatisfyMapper.updateById(patriarchSatisfy);
        return new Result(Code.OK,null,"满意度信息更新完成");
    }
    @DeleteMapping("DeleteSatisfy")
    @ApiImplicitParam("删除满意度信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "id", dataType = "String" , required =false),
    })
    public Result DeleteSatisfy(@RequestParam Map reMap){
        String id = (String) reMap.get("id");
        patriarchSatisfyMapper.deleteById(id);
        return new Result(Code.OK,null,"满意度信息删除完成");
    }

    @GetMapping("/satisfyNumber")
    @ApiOperation("满意度人数统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId" , value = "校区id", dataType = "String" , required =false),
            @ApiImplicitParam(name = "subjectsId" , value = "科目", dataType = "String" , required =false),
            @ApiImplicitParam(name = "yearPart" , value = "年份", dataType = "String" , required =false),
            @ApiImplicitParam(name = "quarterNum" , value = "学期", dataType = "String" , required =false),
            @ApiImplicitParam(name = "type" , value = "类型", dataType = "String" , required =true),
    })
    public Result satisfyNumber(@RequestParam Map reMap){
        String schoolId = (String) reMap.get("schoolId");
        String subjectsId = (String) reMap.get("subjectsId");
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String type = (String) reMap.get("type");

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"ps.school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId),"ps.subjects_id",subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart),"ps.year_part",yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum),"ps.quarter_num",quarterNum);

        //type ：1、个人，2、校区老师，3、校长，4、校区
        if(type.equals("1")){
            queryWrapper.groupBy("g.teacher_id");
        }else if(type.equals("2")){
            queryWrapper.groupBy("g.school_id","g.teacher_id");
        }else if(type.equals("3")){
            queryWrapper.inSql("g.teacher_id","select admin_id from role_admin where role_id = 1568160887369994242 ");
            queryWrapper.groupBy("g.teacher_id");
        }else if(type.equals("4")){
            queryWrapper.groupBy("g.school_id");
        }
        List<SatisfiedStatus> SatisfiedStatusList = gradeMapper.getSatisfiedStatus(queryWrapper);
        for(SatisfiedStatus satisfiedStatus : SatisfiedStatusList){
            satisfiedStatus.setTeacherName(repairCourseMapper.getTeacherName(String.valueOf(satisfiedStatus.getTeacherId())));
            satisfiedStatus.setSchoolName(repairCourseMapper.getSchoolName(satisfiedStatus.getSchoolId()));
            double result = (double) satisfiedStatus.getSatisfiedNumber() / satisfiedStatus.getAllNumber();
            DecimalFormat df = new DecimalFormat("0.00%");
            String formattedResult = df.format(result);
            satisfiedStatus.setSatisfiedPercent(formattedResult);
            double resultRenew = (double) satisfiedStatus.getSummerNumber() / satisfiedStatus.getAllNumber();
            String formattedResultRenew = df.format(resultRenew);
            satisfiedStatus.setRenewPercent(formattedResultRenew);
        }
        return new Result(Code.OK,SatisfiedStatusList,"满意度情况统计完成");
    }


    @PostMapping("getSatisfyStudent")
    @ApiImplicitParam("分页获取满意度列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum" , value = "分页数", dataType = "String" , required =false),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸", dataType = "String" , required =false),
            @ApiImplicitParam(name = "gradeId" , value = "班级", dataType = "String" , required =false),
            @ApiImplicitParam(name = "teacherId" , value = "老师id", dataType = "String" , required =false),
            @ApiImplicitParam(name = "evaluateType" , value = "满意情况", dataType = "String" , required =false)
    })
    public Result getSatisfyStudent(@RequestBody Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<PatriarchSatisfy> iPage = new Page<>(pageNum,pageSize);

        String gradeId = (String) reMap.get("gradeId");
        String evaluateType = (String) reMap.get("evaluateType");
        String nextQuartarNum = (String) reMap.get("nextQuartarNum") ;
        String teacherId = (String) reMap.get("teacherId");
        String schoolId = (String) reMap.get("schoolId");

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(teacherId) , "teacher_id",teacherId);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(evaluateType),"evaluate_type",evaluateType);
        queryWrapper.eq(StringUtils.isNotBlank(nextQuartarNum),"next_quartar_num",nextQuartarNum);
        iPatriarchSatisfyService.page(iPage,queryWrapper);
        List<PatriarchSatisfy> patriarchSatisfyList = iPage.getRecords();
        for(PatriarchSatisfy patriarchSatisfy : patriarchSatisfyList){
            patriarchSatisfy.setTeacherName(repairCourseMapper.getTeacherName(String.valueOf(patriarchSatisfy.getTeacherId())));
        }
        return new Result(Code.OK,iPage,"满意度情况检索完成");
    }

}
